#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <numeric>
using namespace std;
struct edge {
  int u, v, t;
  bool operator<(const edge& rhs) const { return t > rhs.t; }
} e[50005];
struct tree_edge {
  int to, dist, nxt;
} E[120005];
int n, m, fa[10005], head[10005] = {0}, cnt = 0, ans, deep[10005], p[10005][16],
                     sml[10005][16];
int dad(int x) { return (fa[x] == x) ? (x) : (fa[x] = dad(fa[x])); }
inline void addedge(int from, int to, int dist) {
  E[++cnt] = (tree_edge){to, dist, head[from]};
  head[from] = cnt;
  E[++cnt] = (tree_edge){from, dist, head[to]};
  head[to] = cnt;
}
void dfs(int u) {
  for (int i = head[u]; i; i = E[i].nxt)
    if (!deep[E[i].to]) {
      deep[E[i].to] = deep[u] + 1;
      p[E[i].to][0] = u;
      sml[E[i].to][0] = E[i].dist;
      dfs(E[i].to);
    }
}
void init() {
  for (int j = 1; (1 << j) <= n; ++j)
    for (int i = 1; i <= n; ++i)
      if (p[i][j - 1] != -1)
        p[i][j] = p[p[i][j - 1]][j - 1],
        sml[i][j] = min(sml[i][j - 1], sml[p[i][j - 1]][j - 1]);
}
int lca(int x, int y, int& ans) {
  ans = 2000000000;
  int i;
  if (deep[x] < deep[y]) swap(x, y);
  for (i = 0; (1 << i) <= n; ++i)
    ;
  --i;
  for (int j = i; j >= 0; --j)
    if (deep[p[x][j]] >= deep[y]) {
      ans = min(ans, sml[x][j]), x = p[x][j];
    }
  if (x == y) return x;
  for (int j = i; j >= 0; --j)
    if (p[x][j] != p[y][j] && p[x][j] != -1) {
      ans = min(ans, min(sml[x][j], sml[y][j]));
      x = p[x][j];
      y = p[y][j];
    }
  ans = min(ans, min(sml[x][0], sml[y][0]));
  return p[x][0];
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) cin >> e[i].u >> e[i].v >> e[i].t;
  sort(e + 1, e + m + 1);
  for (int i = 1; i <= n; ++i) fa[i] = i;
  for (int okE = 1, now = 1; now <= m; ++now) {
    int a = dad(e[now].u), b = dad(e[now].v);
    if (a != b) {
      fa[b] = a;
      addedge(e[now].u, e[now].v, e[now].t);
      ++okE;
    }
    if (okE == n) break;
  }
  int Q;
  cin >> Q;
  memset(deep, 0, sizeof deep);
  memset(p, -1, sizeof p);
  memset(sml, 0x3f, sizeof sml);
  for (int i = 1; i <= n; ++i)
    if (!deep[i]) {
      deep[i] = 1;
      dfs(i);
    }
  init();
  while (Q--) {
    int x, y;
    cin >> x >> y;
    int a = dad(x), b = dad(y);
    if (a != b) {
      puts("-1");
      continue;
    }
    lca(x, y, ans);
    printf("%d\n", ans);
  }
}
